package com.authine.cloudpivot.ext.applicationservice;

import com.alibaba.cola.dto.RequestContext;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.applicationservice.base.ApplicationBaseService;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.InviteCode;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.cloudpivot.ext.utils.StringUtils;
import com.authine.hermes.app.launcher.org.User;
import com.authine.mvp.app.launcher.common.TableHelper;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.*;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * 个人基础设置
 */
@Path("UserBaseInfoService")
@Slf4j
public class UserBaseInfoService extends ApplicationBaseService {
    private NamedParameterJdbcTemplate jdbcTemplate = ApplicationContextUtils.getBean(NamedParameterJdbcTemplate.class);
    private TableHelper tableHelper = ApplicationContextUtils.getBean(TableHelper.class);

    @Path("getUserInfo")
    @POST
    @Operation(summary = "个人基础设置-个人资料-个人信息",tags = CUSTOM_SERVICE)
    public SingleResponse<Map> getUserInfo() {
        log.info("--------------------个人基础设置-个人资料-个人信息-------------------");
        User user = this.orgService.getUserByUserId(RequestContext.getCorpLoginId()).getData();
        log.info("--------------------个人基础设置-个人资料-个人信息-------------------Result:{}", JSONObject.toJSONString(user));
        if(null == user){
            return SingleResponse.of(new HashMap());
        }else{
            Map<String, Object> userMap = new HashMap<>();
            Map<String, Object> organization = JdbcTemplateUtils.builder(AppConst.SYSTEM_MANAGEMENT, ModelConst.T_ORGANIZATION)
                    .selectField("*").eq("id", user.getDetailInfo().getDepartmentId()).queryForMap();
            String major_product_category = "无";
            String address = "无";
            String major_brand = "无";
            if(null!=organization&&!organization.isEmpty()){
                major_product_category = (String) organization.get("organization_product_line");
                address = (String) organization.get("sales_area");
                major_brand = (String) organization.get("major_brand");
            }
            userMap.put("user_name", user.getDetailInfo().getName());
            userMap.put("email", user.getDetailInfo().getEmail());
            userMap.put("mobile", user.getDetailInfo().getMobile());
            userMap.put("major_product_category", major_product_category);
            userMap.put("address", address);
            userMap.put("major_brand", major_brand);
            return SingleResponse.of(userMap);
        }
    }

    @Path("setDefaultInvoice")
    @POST
    @Operation(summary = "个人基础设置-常用发票信息-设置默认发票",tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<Boolean> setDefaultInvoice(InviteCode dto) {
        log.info("--------------------个人基础设置-常用发票信息-设置默认发票-------------------Param:{}", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "ID必填");
        }
        String tableName = getTableName(ModelConst.T_MEMBER_INVOICE);
        Integer i = updateDefault(tableName, "default_invoice", dto.getId());
        log.info("--------------------个人基础设置-常用发票信息-设置默认发票-------------------Result:{}", JSONObject.toJSONString(i));
        return SingleResponse.of(i>=0?true:false);
    }

    @Path("setDefaultAddress")
    @POST
    @Operation(summary = "个人基础设置-常用地址-设置默认地址",tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<Boolean> setDefaultAddress(InviteCode dto) {
        log.info("--------------------个人基础设置-常用发票信息-设置默认地址 setDefaultAddress-------------------Param:{}", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "ID必填");
        }
        String tableName = getTableName(ModelConst.T_MEMBER_DELIVER_ADDRESS);
        Integer i = updateDefault(tableName, "default_address", dto.getId());
        log.info("--------------------个人基础设置-常用发票信息-设置默认地址 setDefaultAddress-------------------Result:{}", JSONObject.toJSONString(i));
        return SingleResponse.of(i>=0?true:false);
    }

    @Path("setDefaultBankAccount")
    @POST
    @Operation(summary = "个人基础设置-常用收款账号-设置默认收款账户",tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<Boolean> setDefaultBankAccount(InviteCode dto) {
        log.info("--------------------个人基础设置-常用收款账号-设置默认收款账户 setDefaultBankAccount-------------------Param:{}", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "ID必填");
        }
        String tableName = getTableName(ModelConst.T_MEMBER_BANK_ACCOUNT);
        Integer i = updateDefault(tableName, "default_bank_account", dto.getId());
        log.info("--------------------个人基础设置-常用收款账号-设置默认收款账户 setDefaultBankAccount-------------------Result:{}", JSONObject.toJSONString(i));
        return SingleResponse.of(i>=0?true:false);
    }

    public Integer updateDefault(String tableName,String defaultKey, String id){
        if(StringUtils.isBlank(tableName)||StringUtils.isBlank(defaultKey)||StringUtils.isBlank(id)){
            return 0;
        }
        String otherSql = "update "+tableName+" set "+defaultKey+" ='0' where member_id like '%"+RequestContext.getCorpLoginId()+"%'";
        String sql = "update "+tableName+" set "+defaultKey+" ='1' where id='"+id+"'";
        jdbcTemplate.update(otherSql, new HashMap<>());
        Integer i = jdbcTemplate.update(sql, new HashMap<>());
        return i;
    }
}
